package cate.game.stat.item;

import cate.game.framework.ModuleMan;
import cate.game.role.bag.hero.Hero;
import cate.game.role.bag.hero.HeroBag;
import cp.solution.business.stat.item.dto.HeroLogs;
import cp.solution.rpc.path.RpcStatPath;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class StatHeroPlusHandler extends ModuleMan {

    private static final boolean DEBUG = false;

    private static long REPORT_INTERVAL() {
        return DEBUG ? 9_000 : 180_000;
    }

    @Override
    public void initDependent() throws Exception {
    }

    public void tryReport(HeroBag bag) {
        if(game == null) { return; }
        if(bag.changeCommittedStat) {
            return;
        }
        final long now = System.currentTimeMillis();
        if(now - bag.changeCommittedTime < REPORT_INTERVAL()) {
            return;
        }
        bag.changeCommittedTime = now;
        bag.changeCommittedStat = true;
        HeroLogs data = new HeroLogs();

        try {
            data.setRoleId(bag.getRole().getUid());
            data.setSrvId(bag.getRole().getBase().srvId);
            data.setGameId(bag.getRole().getBase().gameId);
            data.setGroupCode(bag.getRole().getGame().config.gs.getGroupCode());

            StatHeroBook book = new StatHeroBook();
            for(Hero hero : bag.getList()) {
                book.read(bag.getRole().getGame(), hero);
            }
            data.setList(book.toList());

            game.rpcSend().fireAndForgetStat(RpcStatPath.HERO_LOGS, data);
        } catch (Exception e) {
            log.error("", e);
        }
    }
}
